const app = getApp();
import { category, save } from '../../../api/mine/feedback';

Page({
  data: {
    back: true,
    title: '反馈建议',
    loading: true,
    submitLoading: false,
    category: [],
    form: {
      category_id: '',
      contact: '',
      content: ''
    }
  },
  onLoad(options) {
    this.setData({ loading: true });
    let form = this.data.form;
    category().then(res => {
      let data = res.data;
      form.category_id = data[0].id;
      this.setData({
        category: data,
        form: form
      });
    })
      .finally(() => {
        this.setData({ loading: false });
      });
  },
  onPullDownRefresh() {/*监听用户下拉动作*/ },

  //===================methon================

  setContact(event) {
    let form = this.data.form;
    form.contact = event.detail;
    this.setData({ form: form });
  },

  setContent(event) {
    let form = this.data.form;
    form.content = event.detail;
    this.setData({ form: form });
  },

  handleSubmit() {
    const form = this.data.form;
    if (form.category_id == 0) {
      app.toast('请选择问题分类');
      return false;
    }
    if (form.content.length < 10) {
      app.toast('请输入至少10个字以上的问题描述');
      return false;
    }
    if (form.contact == '') {
      app.toast('请输入您的联系方式');
      return false;
    }
    this.setData({ submitLoading: true });
    save(form).then(res => {
      app.toast('提交成功', function () {
        wx.navigateBack();
      }, 'success');
    })
      .finally(() => {
        this.setData({
          submitLoading: false
        });
      });
  },

  onChange(event) {
    let form = this.data.form;
    form.category_id = event.detail;
    this.setData({
      form: form
    });
  },

  onClick(event) {
    let form = this.data.form;
    form.category_id = event.currentTarget.dataset.name;
    this.setData({
      form: form
    });
  }
})